package com.share.finance.asset.sjd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.finance.asset.sjd.mapper.AccessLogMapper;
import com.share.finance.asset.sjd.model.db.AccessLog;
import com.share.finance.asset.sjd.model.vo.LogPageReq;
import com.share.finance.asset.sjd.service.IAccessLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-29
 */
@Service
public class AccessLogServiceImpl extends ServiceImpl<AccessLogMapper, AccessLog> implements IAccessLogService {

    @Resource
    private AccessLogMapper accessLogMapper;

    @Override
    public Page pageList(LogPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<AccessLog> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getResponse())) {
            queryWrapper.like(AccessLog::getResponse, req.getResponse());
        }
        if (StringUtils.isNotBlank(req.getRequest())) {
            queryWrapper.like(AccessLog::getRequest, req.getRequest());
        }
        if (StringUtils.isNotBlank(req.getService())) {
            queryWrapper.like(AccessLog::getService, req.getService());
        }
        if (StringUtils.isNotBlank(req.getUri())) {
            queryWrapper.like(AccessLog::getUri, req.getUri());
        }
        if (StringUtils.isNotBlank(req.getIp())) {
            queryWrapper.like(AccessLog::getIp, req.getIp());
        }
        if (StringUtils.isNotBlank(req.getMethod())) {
            queryWrapper.like(AccessLog::getMethod, req.getMethod());
        }
        queryWrapper.orderByDesc(AccessLog::getId);
        List<AccessLog> list = accessLogMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }
}
